package com.xkdx.caipiao;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.my.OrderDetailsActivity;
import com.xkdx.caipiao.presistence.historynewaccount.HistoryNewAccountQueryInfo;
import com.xkdx.caipiao.presistence.historynewaccount.HistoryNewAccountQueryModule;
import com.xkdx.caipiao.presistence.historynewaccount.HistoryNewAccountQueryPresistence;
import com.xkdx.caipiao.presistence.historynewaccount.HistoryNewAcountAction;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;

public class MyAccountNewActivity extends ThreadActivity implements
		OnPageChangeListener, OnClickListener {

	ViewPager viewPager;
	TextView tv_all, tv_goucai, tv_chongzhi, tv_tixian, tv_jiangjin;
	ListView lv_all, lv_goucai, lv_chongzhi, lv_tixian, lv_jiangjin;
	View view_all, view_goucai, view_chongzhi, view_tixian, view_jiangjin;
	List<View> viewList;
	List<HistoryNewAccountQueryInfo> list;
	List<HistoryNewAccountQueryInfo> list_goucai, list_chongzhi, list_tixian,
			list_jiangjin;
	HistoryNewAcountAction action;
	HistoryNewAccountQueryModule module;
	HistoryNewAccountQueryPresistence presistence;
	private String devicetype = "2";
	Drawable im_bottom;
	String tag = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_new_account_caipiao);
		im_bottom = getResources().getDrawable(R.drawable.viewpagger_line_caipiao);
		im_bottom.setBounds(0, 0, im_bottom.getMinimumWidth(),
				im_bottom.getMinimumHeight());
		tag = getIntent().getStringExtra("tag");
		findView();
		startThread();
	}

	private void findView() {

		tv_goucai = (TextView) findViewById(R.id.text2);
		tv_chongzhi = (TextView) findViewById(R.id.text3);

		tv_goucai.setOnClickListener(this);
		tv_chongzhi.setOnClickListener(this);

		viewPager = (ViewPager) findViewById(R.id.vPager);
		LayoutInflater lf = LayoutInflater.from(MyAccountNewActivity.this);
		list_goucai = new ArrayList<HistoryNewAccountQueryInfo>();
		list_chongzhi = new ArrayList<HistoryNewAccountQueryInfo>();

		view_goucai = lf.inflate(R.layout.goucai_account_caipiao, null);
		view_chongzhi = lf.inflate(R.layout.chongzhi_account_caipiao, null);

		viewList = new ArrayList<View>();// 将要分页显示的View装入数组中

		viewList.add(view_goucai);
		viewList.add(view_chongzhi);

		lv_goucai = (ListView) view_goucai.findViewById(R.id.goucai_account_ls);
		lv_chongzhi = (ListView) view_chongzhi
				.findViewById(R.id.chongzhi_account_ls);

		viewPager.setAdapter(new adapter());

		viewPager.setOnPageChangeListener(this);
		if (!(TextUtils.isEmpty(tag)) && "tixian".equals(tag)) {
			viewPager.setCurrentItem(3);
		} else {
			viewPager.setCurrentItem(0);
		}

		/*lv_goucai.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(MyAccountNewActivity.this,
						OrderDetailsActivity.class);
				intent.putExtra("orderid", list_goucai.get(arg2).getOid());
				startActivity(intent);
			}
		});*/
		lv_chongzhi.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			}
		});

	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {

			list = module.list;
			if (list != null && list.size() > 0) {

				setList();
			} else {

			}

		} else {
			Toast.makeText(MyAccountNewActivity.this, "获取账户收支列表失败",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void setList() {

		for (HistoryNewAccountQueryInfo info : list) {
			if (info.getType().equals("1") || info.getType().equals("5")) {
				list_goucai.add(info);
			} else if (info.getType().equals("2") || info.getType().equals("4")) {
				list_chongzhi.add(info);
			}
		}

		listAdapter toucaiadapter = new listAdapter(list_goucai);
		lv_goucai.setAdapter(toucaiadapter);

		listAdapter chongzhiAdapter = new listAdapter(list_chongzhi);
		lv_chongzhi.setAdapter(chongzhiAdapter);

	}

	@Override
	public void startThread() {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		LoginInfo info = getLoginInfo();
		String md5 = info.getId()+info.getLogintoken()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action = new HistoryNewAcountAction(info.getId(), info.getLogintoken(),
				"0", "1", "1000",devicetype,signcode);
		module = new HistoryNewAccountQueryModule();
		presistence = new HistoryNewAccountQueryPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();

	}

	@Override
	public void onBackClick(View view) {
		finish();
	}

	class adapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 2;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {

			container.removeView(viewList.get(position));

		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(viewList.get(position));// 添加页卡
			return viewList.get(position);
		}

	}

	class listAdapter extends BaseAdapter {
		List<HistoryNewAccountQueryInfo> list;

		public listAdapter(List<HistoryNewAccountQueryInfo> list) {
			// TODO Auto-generated constructor stub
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertview, ViewGroup arg2) {
			viewHolder holder;
			if (convertview == null) {
				holder = new viewHolder();
				convertview = LayoutInflater.from(MyAccountNewActivity.this)
						.inflate(R.layout.my_account_item_caipiao, null);
				holder.time = (TextView) convertview.findViewById(R.id.time);
				holder.money = (TextView) convertview.findViewById(R.id.money);
				holder.type = (TextView) convertview.findViewById(R.id.type);

				convertview.setTag(holder);
			} else {
				holder = (viewHolder) convertview.getTag();
			}

			float mon = Float.valueOf(list.get(position).getMoney()) / 100;
			DecimalFormat df = new DecimalFormat("#0.00");
			String money = df.format(mon);

			holder.money.setText(money + "元");
			holder.time.setText(list.get(position).getCtime());
			String status = list.get(position).getType();
			String detail = list.get(position).getDetail();
			if (status.equals("1")) {
				holder.type.setText("购彩");
			} else if (status.equals("2")) {
				holder.type.setText("充值");
			} else if (status.equals("5")) {
				holder.type.setText("手机话费");
			} else if (status.equals("4")) {
				holder.type.setText("奖金");
			}

			return convertview;
		}

		class viewHolder {
			TextView time, money, type;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int id) {
		switch (id) {

		case 0:

			tv_chongzhi.setCompoundDrawables(null, null, null, null);

			tv_goucai.setCompoundDrawables(null, null, null, im_bottom);

			break;
		case 1:

			tv_chongzhi.setCompoundDrawables(null, null, null, im_bottom);
			tv_goucai.setCompoundDrawables(null, null, null, null);

			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {

		case R.id.text2:
			viewPager.setCurrentItem(0);
			break;
		case R.id.text3:
			viewPager.setCurrentItem(1);
			break;

		default:
			break;
		}
	}

}
